package com.alumni_circle.utils;

import cn.hutool.core.util.ObjectUtil;
import com.alumni_circle.core.utils.SecurityUtils;
import com.alumni_circle.entity.home.issue.UserClub;
import com.alumni_circle.service.home.issue.impl.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author 龙小虬
 * @since 2020-08-13 22:50
 */
@Component
public class MsgPermissions {

    @Resource
    UserNewsServiceImpl userNewsService;

    @Resource
    UserClubServiceImpl userClubService;

    @Resource
    UserSponsorServiceImpl userSponsorService;

    @Resource
    UserLostServiceImpl userLostService;

    @Resource
    UserPartServiceImpl userPartService;

    @Resource
    UserGossipServiceImpl userGossipService;

    public boolean updateDelete(String articleId,String article){
        if("ROLE_ADMIN".equals(SecurityUtils.getRole())){
        }else {
            QueryWrapper queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_id",SecurityUtils.getId());
            if ("news".equals(article)) {
                queryWrapper.eq("news_id",articleId);
                return ObjectUtil.hasEmpty(userNewsService.getOne(queryWrapper));
            } else if ("sponsor".equals(article)) {
                queryWrapper.eq("sponsor_id",articleId);
                return ObjectUtil.hasEmpty(userSponsorService.getOne(queryWrapper));
            } else if ("lost".equals(article)) {
                queryWrapper.eq("lost_id",articleId);
                return ObjectUtil.hasEmpty(userLostService.getOne(queryWrapper));
            } else if ("part".equals(article)) {
                queryWrapper.eq("part_id",articleId);
                return ObjectUtil.hasEmpty(userPartService.getOne(queryWrapper));
            } else if ("gossip".equals(article)) {
                queryWrapper.eq("gossip_id",articleId);
                return ObjectUtil.hasEmpty(userGossipService.getOne(queryWrapper));
            }
        }
        return false;
    }

    public boolean updateDelete(String articleId,String article,String clubName){

        if ("club".equals(article)) {
            String role = SecurityUtils.getRole();
            if (role.contains("ROLE_ADMIN") || role.contains("ROLE_CLUBADMIN")){
                return true;
            }
            QueryWrapper<UserClub> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_id", SecurityUtils.getId());
            queryWrapper.eq("clubActivity_id", articleId);
            return ObjectUtil.hasEmpty(userClubService.getOne(queryWrapper));
        }
        return false;
    }

}
